﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KYJControlLib
{
    /// <summary>
    /// 所有窗体的枚举
    /// </summary>
    /// <remarks>
    /// 小于临界窗体，不需要关闭的，是固定窗体
    /// </remarks>
    public enum FormName
    {
        控制流程,
        日志报警,
        临界窗体,
        日志查询,
        参数设置,
        实时趋势,
        历史趋势,
        数据报表,
        用户管理,
        退出系统
    }

    public partial class NaviPanel : UserControl
    {
        public NaviPanel()
        {
            InitializeComponent();
            //绑定按钮点击事件
            BindEvent();
        }
        [Browsable(true)]
        [Category("自定义事件")]
        [Description("按钮选择切换事件")]
        public event Func<object, EventArgs, bool> ButtonSelectEvent;
        /// <summary>
        /// 按钮选择通用事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ButtonSelect_Click(object sender, EventArgs e)
        {
            if (sender is Button button)
            {
                if (Enum.IsDefined(typeof(FormName), button.Text))//在枚举中是否定义
                {
                    FormName formName = (FormName)Enum.Parse(typeof(FormName), button.Text);
                    if (ButtonSelectEvent != null && ButtonSelectEvent(formName, e) == true)
                    {
                        //改变对应按钮的颜色
                        SetButtom(formName);
                    }
                }
            }
        }
        /// <summary>
        ///通过代码去做按钮事件绑定
        /// </summary>
        private void BindEvent()
        {
            foreach (var item in this.panelEnhanced1.Controls)
            {
                if (item is Button button)
                {
                    if (Enum.IsDefined(typeof(FormName), button.Text))//在枚举中是否定义
                    {
                        button.Click += btn_ButtonSelect_Click;
                    }
                }
            }
        }
        /// <summary>
        /// 设置按钮切换的状态
        /// </summary>
        /// <param name="formName"></param>
        private void SetButtom(FormName formName)
        {
            foreach (var item in this.panelEnhanced1.Controls)
            {
                if (item is Button button)
                {
                    if (button.Text == formName.ToString())
                    {
                        button.Font = new System.Drawing.Font("微软雅黑", 11F, FontStyle.Bold);
                        button.ForeColor = System.Drawing.Color.Cyan;
                    }
                    else
                    {
                        button.Font = new System.Drawing.Font("微软雅黑", 11.25F, FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                        button.ForeColor = System.Drawing.Color.White;
                    }
                }
            }
        }
    }
}
